home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11185 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1003 b 

  1. Path: newserv.agcs.com!news
  2. From: "George B. T. Greene" <greeneg@agcs.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Rand() Function
  5. Date: 22 Mar 1996 14:21:11 GMT
  6. Organization: AG Communication Systems, ITS
  7. Message-ID: <4iud0n$dks@newserv.agcs.com>
  8. References: <4iqltj$40g@news1.sympatico.ca>
  9. NNTP-Posting-Host: greeneg.agcs.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.2N (Windows; I; 32bit)
  14.  
  15. Gisele Swinson <gisele.swinson@sympatico.ca> wrote:
  16. >How do I get a random number from a "SET" of numbers.
  17.  
  18. .. depends a bit on the set :-)
  19.  
  20. >e.g.
  21. >      I want a random number from this set:
  22. >
  23. >      2, 4, 6, 8, 10
  24.  
  25. OK, for a small set like this, assuming rand( ) is good enough for your 
  26. purposes, you could try (this approach will work for _any_ "small" set):
  27.  
  28. #define NUMELEMENTS 5
  29. int set[NUMELEMENTS] = {2, 4, 6, 8, 10};
  30. int n;
  31. ..
  32. n = set[rand( ) % NUMELEMENTS];
  33.  
  34. or for that particular case, more simply:
  35.  
  36. n = (rand( ) % 5 + 1) * 2;
  37.  
  38.